package PHANVANVUONG.SERLALIZABLE.DEMO;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import PHANVANVUONG.XML.DEMO.Student;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;

@SuppressWarnings({ "unused", "serial" })
public class Views extends JFrame {

	private JPanel contentPane;
	private JTextField Masv;
	private JTextField fullName;
	private JTextField mark;
	private List<StudentEntity> lisObj;
	private StudentEntity objS;
	private JTextField KQ;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Views frame = new Views();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Views() {
		setResizable(false);
		setIconImage(Toolkit.getDefaultToolkit().getImage(Views.class.getResource("/javax/swing/plaf/metal/icons/ocean/menu.gif")));
		setTitle("Lưu thông tin sinh viên");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 454, 348);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "L\u01B0u th\u00F4ng tin sinh vi\u00EAn", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Mã Sinh Viên");
		lblNewLabel.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblNewLabel.setBounds(46, 53, 98, 18);
		contentPane.add(lblNewLabel);
		
		JLabel lblHVTn = new JLabel("Họ và tên");
		lblHVTn.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblHVTn.setBounds(46, 92, 98, 18);
		contentPane.add(lblHVTn);
		
		JLabel lblimS = new JLabel("Điểm số");
		lblimS.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblimS.setBounds(46, 132, 98, 18);
		contentPane.add(lblimS);
		
		Masv = new JTextField();
		Masv.setToolTipText("Chỉ nhập kiểu số");
		Masv.setBounds(175, 53, 232, 20);
		contentPane.add(Masv);
		Masv.setColumns(10);
		
		fullName = new JTextField();
		fullName.setColumns(10);
		fullName.setBounds(175, 92, 232, 20);
		contentPane.add(fullName);
		
		mark = new JTextField();
		mark.setColumns(10);
		mark.setBounds(175, 132, 232, 20);
		contentPane.add(mark);
		
		lisObj=new ArrayList<StudentEntity>();
		
		
		JButton btnNewButton = new JButton("Lưu xuống file");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				JFileChooser objFile = new JFileChooser();
                objFile.setCurrentDirectory(new java.io.File("."));
                objFile.setDialogTitle("Chọn file: ");
                objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                String[] EXTENSION = new String[] { "dat" };
                FileNameExtensionFilter filter = new FileNameExtensionFilter("DAT files", EXTENSION);
                objFile.setFileFilter(filter);
                objFile.setMultiSelectionEnabled(false);

                int returnVal = objFile.showSaveDialog(getParent());
                String ParentPath = "";
                String FileName = "";
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    ParentPath = objFile.getSelectedFile().getParent();
                    FileName = objFile.getSelectedFile().getName();
                    Controllers<StudentEntity> objC=new Controllers<StudentEntity>();
                    boolean b;
                    try{
                    	b=objC.writeFile(lisObj, ParentPath, FileName);
                    	lisObj.clear();
                    }catch(Exception e){
                    	e.printStackTrace();
                    	b=false;
                    }
                    
                    if(b){
                    	KQ.setText("Ghi file thành công");
                    }else{
                    	KQ.setText("Ghi file thất bại");
                    }
                }
                
                
			}
		});
		btnNewButton.setFont(new Font("Cambria", Font.PLAIN, 14));
		btnNewButton.setBounds(175, 263, 127, 23);
		contentPane.add(btnNewButton);
		
		JButton btnThmSv = new JButton("Thêm SV");
		btnThmSv.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(Masv.getText().isEmpty() || fullName.getText().isEmpty() || mark.getText().isEmpty()){
					JOptionPane.showMessageDialog(getParent(), "Vui lòng nhập đầy đủ thông tin", "Thông báo", JOptionPane.ERROR_MESSAGE);
				}else{
					objS = new StudentEntity();
					objS.setRollNo(Masv.getText());
					objS.setFullName(fullName.getText());
					objS.setMark(Double.parseDouble(mark.getText()));
					
					lisObj.add(objS);
					
					KQ.setText("Có: "+lisObj.size()+" sinh viên trong danh sách");
					
					Masv.setText("");
					fullName.setText("");
					mark.setText("");
				}
			}
		});
		btnThmSv.setFont(new Font("Cambria", Font.PLAIN, 14));
		btnThmSv.setBounds(312, 263, 95, 23);
		contentPane.add(btnThmSv);
		
		JLabel lblKtQu = new JLabel("Kết quả");
		lblKtQu.setFont(new Font("Cambria", Font.PLAIN, 14));
		lblKtQu.setBounds(46, 194, 98, 18);
		contentPane.add(lblKtQu);
		
		KQ = new JTextField();
		KQ.setEditable(false);
		KQ.setBounds(175, 176, 232, 65);
		contentPane.add(KQ);
		KQ.setColumns(10);
	}
}
